<?php

class Forum
{
	private $Database;
	private $error = array();
	
	public function __construct(Database $db)
	{
		$this->Database = $db;
	}
	
	/**
	 * Returns an array of error messages
	 * @return array
	 */
	public getErrorMessages()
	{
		return $this->error;
	}
	
	/**
	 * Get categories
	 * @param int
	 * @return array
	 */
	public function getCategories($parentID = 0)
	{
		// Fetch all forums with pid = $parentID
		$sql = 'SELECT *
				FROM ' . CBB_CATEGORIES . "
				WHERE pid = $parent_id";
		
		// TODO
		
		$cat = array(); // TODO: DB
		
		return $cat;
	}
	
	/**
	 * Add new category
	 * @param string
	 * @param string
	 * @param int
	 * @param int
	 * @param string
	 * @param string
	 * @return int
	 */
	public function addCatagory($forumName, $forumDesc = '', $parentID = 0, $sorting = 0, $forumLink = '', $forumImage = '')
	{
		if ($forumName == '')
		{
			$this->error[] = 'NO_FORUM_NAME';
			return 0;
		}
		
		$set = array(
			'pid'						=> (int) $parentID,
			'forum_name'				=> $forumName,
			'forum_desc'				=> $forumDesc,
			'forum_link'				=> $forumLink,
			'forum_image'				=> $forumImage,
			'forum_status'				=> 0, // TODO
			'forum_posts'				=> 0,
			'forum_topics'				=> 0,
			'forum_last_post_id'		=> 0,
			'forum_last_poster_id'		=> 0,
			'forum_last_post_subject'	=> '',
			'forum_last_post_time'		=> 0,
			'forum_last_poster_name'	=> '',
			'forum_last_poster_colour'	=> '',
			'forum_flags'				=> 0, // TODO
			'sorting'					=> (int) $sorting,
			'tstamp'					=> time(),
		);
		
		$insertId = $this->Database->prepare('INSERT INTO ' . CBB_CATEGORIES . ' %s')->set($set)->execute()->insertId;
		
		return $insertId;
	}
	
	/**
	 * Edit category
	 */
	public function editCategory($forumID, $forumName, $forumDesc = '', $parentID = 0, $sorting = 0, $forumLink = '', $forumImage = '')
	{
		if (!$forumID)
		{
			$this->error[] = 'NO_FORUM_ID';
		}
		
		if ($forumName == '')
		{
			$this->error[] = 'NO_FORUM_NAME';
		}
		
		if (!empty($this->error))
		{
			return false;
		}
		
		$set = array(
				'forum_id'					=> (int) $forumID,
				'pid'						=> (int) $parentID,
				'forum_name'				=> $forumName,
				'forum_desc'				=> $forumDesc,
				'forum_link'				=> $forumLink,
				'forum_image'				=> $forumImage,
				'forum_status'				=> 0, // TODO
				'forum_posts'				=> 0,
				'forum_topics'				=> 0,
				'forum_last_post_id'		=> 0,
				'forum_last_poster_id'		=> 0,
				'forum_last_post_subject'	=> '',
				'forum_last_post_time'		=> 0,
				'forum_last_poster_name'	=> '',
				'forum_last_poster_colour'	=> '',
				'forum_flags'				=> 0, // TODO
				'sorting'					=> (int) $sorting,
				'tstamp'					=> time()
		);
		
		$this->Database->prepare('UPDATE ' . CBB_CATEGORIES . ' %s WHERE forum_id = ?')->set($set)->execute($forumID);
		
		return true;
	}
	
	/**
	 * Remove category
	 */
	public function removeCategory($forumID)
	{
		$this->prepare('REMOVE FROM ' . CBB_CATEGORIES . ' WHERE forum_id = ?')->execute($forumID);
	}
	
	/**
	 * Get category's contents
	 */
	public function getTopics($forumID)
	{
		// Fetch all threads with forum_id = $forumID
		$contents = array(); // TO DO: DB
	
		return $contents;
	}
}

?>